home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / CBSETKCU.C < prev    next >
Text File  |  1991-09-23  |  3KB  |  148 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbsetkcu.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDLIB
  11. #include <stdlib.h>
  12. #endif
  13. #ifdef AC_STRING
  14. #include <string.h>
  15. #endif
  16.  
  17. /* library headers */
  18. #include <blkio.h>
  19. #include <btree.h>
  20. #include <lseq.h>
  21.  
  22. /* local headers */
  23. #include "cbase_.h"
  24.  
  25. /*man---------------------------------------------------------------------------
  26. NAME
  27.      cbsetkcur - set cbase key cursor
  28.  
  29. SYNOPSIS
  30.      #include <cbase.h>
  31.  
  32.      int cbsetkcur(cbp, field, cbkposp)
  33.      cbase_t *cbp;
  34.      int field;
  35.      const cbkpos_t *cbkposp;
  36.  
  37. DESCRIPTION
  38.      The cbsetkcur function sets the position of a key cursor of the
  39.      specified field in cbase cbp to the value pointed to by cbkposp.
  40.      If cbkposp is the NULL pointer, the key cursor is set to null.
  41.      The record cursor of cbp is positioned to the record associated
  42.      with that key.  Other key cursors are not affected.
  43.  
  44.      cbsetkcur will fail if one or more of the following is true:
  45.  
  46.      [EINVAL]       cbp is not a valid cbase pointer.
  47.      [EINVAL]       field is not a valid field number for cbase cbp.
  48.      [CBELOCK]      cbp is not locked.
  49.      [CBENOPEN]     cbp is not open.
  50.  
  51. SEE ALSO
  52.      cbgetkcur, cbsetrcur.
  53.  
  54. DIAGNOSTICS
  55.      Upon successful completion, a value of 0 is returned.  Otherwise,
  56.      a value of -1 is returned, and errno set to indicate the error.
  57.  
  58. ------------------------------------------------------------------------------*/
  59. #ifdef AC_PROTO
  60. int cbsetkcur(cbase_t *cbp, int field, const cbkpos_t *cbkposp)
  61. #else
  62. int cbsetkcur(cbp, field, cbkposp)
  63. cbase_t *cbp;
  64. int field;
  65. const cbkpos_t *cbkposp;
  66. #endif
  67. {
  68.     void *    buf    = NULL;
  69.     cbrpos_t cbrpos    = NIL;
  70.     lspos_t    lspos    = NIL;
  71.     btpos_t    btpos;
  72.  
  73.     /* initialize automatic aggregates */
  74.     memset(&btpos, 0, sizeof(btpos));
  75.  
  76.     /* validate arguments */
  77.     if (!cb_valid(cbp)) {
  78.         errno = EINVAL;
  79.         return -1;
  80.     }
  81.  
  82.     /* check if not open */
  83.     if (!(cbp->flags & CBOPEN)) {
  84.         errno = CBENOPEN;
  85.         return -1;
  86.     }
  87.  
  88.     /* validate arguments */
  89.     if (field < 0 || field >= cbp->fldc) {
  90.         errno = EINVAL;
  91.         return -1;
  92.     }
  93.  
  94.     /* check if not locked */
  95.     if (!(cbp->flags & CBLOCKS)) {
  96.         errno = CBELOCK;
  97.         return -1;
  98.     }
  99.  
  100.     /* set key cursor position */
  101.     if (cbkposp == NULL) {
  102.         if (btsetcur(cbp->btpv[field], NULL) == -1) {
  103.             CBEPRINT;
  104.             return -1;
  105.         }
  106.     } else {
  107.         memcpy(&btpos, cbkposp, sizeof(btpos));
  108.         if (btsetcur(cbp->btpv[field], &btpos) == -1 ) {
  109.             CBEPRINT;
  110.             return -1;
  111.         }
  112.     }
  113.  
  114.     /* check if key cursor is null */
  115.     if (btcursor(cbp->btpv[field]) == NULL) {
  116.         if (lssetcur(cbp->lsp, NULL) == -1) {
  117.             CBEPRINT;
  118.             return -1;
  119.         }
  120.         return 0;
  121.     }
  122.  
  123.     /* get record position */
  124.     buf = calloc((size_t)1, btkeysize(cbp->btpv[field]));
  125.     if (btgetk(cbp->btpv[field], buf) == -1) {
  126.         CBEPRINT;
  127.         free(buf);
  128.         return -1;
  129.     }
  130.     memcpy(&cbrpos, (char *)buf + cbp->fldv[field].len, sizeof(cbrpos));
  131.     free(buf);
  132.     buf = NULL;
  133.  
  134.     /* set record cursor position */
  135.     lspos = cbrpos;
  136.     if (lspos == NIL) {
  137.         CBEPRINT;
  138.         errno = CBEPANIC;
  139.         return -1;
  140.     }
  141.     if (lssetcur(cbp->lsp, &lspos) == -1) {
  142.         CBEPRINT;
  143.         return -1;
  144.     }
  145.  
  146.     return 0;
  147. }
  148.